package com.tsinghua.discharge.service.impl;

import com.tsinghua.discharge.common.Const;
import com.tsinghua.discharge.common.ServerResponse;
import com.tsinghua.discharge.dao.*;
import com.tsinghua.discharge.model.*;
import com.tsinghua.discharge.service.DeleteFileService;
import com.tsinghua.discharge.service.FileUploadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class DeleteFileServiceImpl implements DeleteFileService{

    @Autowired
    FileUploadRecordService fileUploadRecordService;
    @Autowired
    GcamMappingMapper gcamMappingMapper;
    @Autowired
    GcamBaseMapper gcamBaseMapper;
    @Autowired
    GcamResultMapper gcamResultMapper;
    @Autowired
    RegionMappingMapper regionMappingMapper;
    @Autowired
    ThuMappingMapper thuMappingMapper;
    @Autowired
    EnergyBaseMapper energyBaseMapper;
    @Autowired
    EnergyProportionMapper energyProportionMapper;
    @Autowired
    SectorMapper sectorMapper;

    @Override
    public ServerResponse deleteByFileType(String fileType,String supSector,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        Integer userId=user.getId();

        if(fileType.equals(Const.DataType.GCAMMAPPING) || fileType.equals(Const.DataType.MODELDATA)|| fileType.equals(Const.DataType.REGION)
                || fileType.equals(Const.DataType.THU_PROP)|| fileType.equals(Const.DataType.THU_MAPPING)
                && fileType!=null&&supSector!=null) {
            ServerResponse check=checkSector(supSector,session);
            if(!check.isSuccess()){
                return ServerResponse.createByErrorMessage("无此顶级部门");
            }
            ServerResponse response = fileUploadRecordService.deleteRecordByFileTypeAndSector(fileType,supSector, userId);
            int affectCount=0;
            switch (fileType){
                case Const.DataType.GCAMMAPPING:
                    affectCount=gcamMappingMapper.deleteBySector(supSector,userId);
                    break;
                case Const.DataType.MODELDATA:
                    List<GcamBase> gcamBases=gcamBaseMapper.selectBySector(supSector,userId);
                    for(GcamBase base:gcamBases){
                        affectCount=gcamResultMapper.deleteByGcamId(base.getGcamId(),userId);
                        if(affectCount<=0){
                            ServerResponse.createByErrorMessage("删除GCAMBASE数据失败");
                        }
                    }
                    affectCount=gcamBaseMapper.deleteBySector(supSector,userId);
                    break;
                case Const.DataType.REGION:
                    affectCount=regionMappingMapper.deleteByUser(userId);
                    break;
                case Const.DataType.THU_MAPPING:
                    affectCount=thuMappingMapper.deleteBySupsectorAndUser(supSector,userId);
                    break;
                case Const.DataType.THU_PROP :
                    List<EnergyBase> energyBaseList=energyBaseMapper.selectBySupsectorAndUser(supSector,userId);
                    for(EnergyBase energyBase:energyBaseList){
                        affectCount=energyProportionMapper.deleteById(energyBase.getEnergyId(),userId);
                        if(affectCount<=0){
                            ServerResponse.createByErrorMessage("删除能源比例数据失败");
                        }
                    }
                    affectCount=energyBaseMapper.deleteBySector(supSector,userId);
                    break;
                default:
                    ServerResponse.createByErrorMessage("类型不匹配");
                    break;
            }
            if(affectCount<=0&& !response.isSuccess()){
                ServerResponse.createByErrorMessage("删除数据失败");
            }

            return ServerResponse.createBySuccessMessage("删除该类型文件成功");
        }


        return ServerResponse.createByErrorMessage("删除文件类型不匹配");
    }

    public ServerResponse checkSector(String supSector,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        Integer userId=user.getId();
        List<String> sup =sectorMapper.selectCheckSector(userId);

        if(sup.size()<=0 ){
            return ServerResponse.createByErrorMessage("无顶级部门数据");
        }
        for(String sec:sup){
            if(supSector.equals(sec)){
                return ServerResponse.createBySuccess();
            }
        }
        return ServerResponse.createByError();
    }

}
